package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @author Mr.Chen
     * @create 2025/3/24
     **/
    @Override
    public void save(SetmealDTO setmealDTO) {
        //1.向setmeal表中添加套餐信息，并生成主键id
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        //2.向setmeal_dish表中添加菜品和套餐关系。
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId = setmeal.getId();
        setmealDishes.forEach(d -> {
            d.setSetmealId(setmealId);//给套餐里的菜品添加setmealId。
           setmealDishMapper.insert(d);
        });

    }

    /**
     *
     * 套餐分页查询
     * @param setmealPageQueryDTO
     *
     **/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //查询
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     *
     **/
    @Override
    public void deleteByIds(List<Long> ids) {
        //删除套餐，首先要判断是否为在售状态。
        //除了要删除setmeal表中套餐信息外，还要删除setmeal_dish表中套餐与菜品的关系
        //售卖状态,若套餐为起售状态则不能删除
        ids.forEach(id -> {
           Setmeal setmeal = setmealMapper.queryById(id);
           if (setmeal.getStatus() == StatusConstant.ENABLE) {
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }
        });
        //1.批量删除套餐
        setmealMapper.deleteBatchByIds(ids);

        //2.批量删除关联信息
        setmealDishMapper.deleteBatchBySetmealIds(ids);

    }

    /**
     * 根据id查询套餐
     *
     * @param id
     *
     **/
    @Override
    public SetmealVO queryById(Long id) {
        //查询setmeal
        SetmealVO setmealVO = setmealMapper.queryByIdVO(id);
        //查询setmeal_dish
        List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     *
     **/
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐首先需要回显，当点击编辑按钮的时候相当于根据id查询操作
        //然后再进行更新操作

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //既要更新setmeal也要更新setmeal_dish
        //更新setmeal
        setmealMapper.update(setmeal);

        //更新setmeal_dish
        //更新套餐中的菜品有多种可能，我们直接将所有的菜品删除之后重新添加即可。
        //根据套餐Id删除菜品信息
        List<Long> list = new ArrayList<>();
        Long setmealId = setmeal.getId();
        list.add(setmealId);
        setmealDishMapper.deleteBatchBySetmealIds(list);
        //添加菜品信息
        setmealDTO.getSetmealDishes().forEach(d -> {
            d.setSetmealId(setmealId);//给套餐里的菜品添加setmealId。
           setmealDishMapper.insert(d);
        });
    }

    /**
     * 套餐起售和停售
     *
     * @param status
     * @param id
     *
     **/
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        if(status == StatusConstant.DISABLE) {
            setmealMapper.update(setmeal);
        } else {
            //如果向将套餐起售，要保证其里面的菜品都处于起售状态。
            //需要根据套餐id查找所有的菜品信息，并判断其售卖状态。
            //首先根据setmealId找到所有dishId，然后查找所有dish的status
            List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(id);
            List<Long> dishIds = new ArrayList<>();
            setmealDishes.forEach(d -> {
                dishIds.add(d.getDishId());
            });
            //通过菜品批量查询数据库获得菜品状态信息
            List<Integer> statuss  = dishMapper.queryByIds(dishIds);
            statuss.forEach(d -> {
                if(d == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
            setmealMapper.update(setmeal);
        }
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
